package com.codeying.servlet.userComment;

import com.codeying.entity.*;
import com.codeying.servlet.AbsServlet;
import com.codeying.utils.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;
import java.util.*;
import com.codeying.utils.component.*;

/**
 * 查询评论
 */
@WebServlet("/userComment/list")
public class ListUserCommentServlet  extends AbsServlet {

    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        LoginUser loginUser = MyUtils.getUser(req);
        req.setCharacterEncoding("utf-8");
        //获取查询条件
        Map<String,String> paramMap = toParamMap(req);
        paramMap.put("pageSize","10");
        //根据查询条件获取分页的数据
        List<UserComment> userCommentList = userCommentService.list(paramMap);

        //获取分页
        PagerVO<UserComment> pagerVO = new PagerVO<>( Integer.parseInt( paramMap.get("pageIndex")),Integer.parseInt( paramMap.get("pageSize")));
        pagerVO.setItemCount(userCommentService.count(paramMap));
        pagerVO.init();
        req.setAttribute("pager",pagerVO);

        //循环遍历list数据，统计、获取外键数据
        for(UserComment stAv : userCommentList){
                                            }
        //如果有信息需要统计，放在这里
        StringBuilder statisticInfo = new StringBuilder();
        req.setAttribute("statisticInfo", statisticInfo.toString());
        req.setAttribute("userCommentList",userCommentList);
        req.getRequestDispatcher("/pages/userComment-list.jsp").forward(req,resp);
    }

    //获取jsp过来的参数
    public Map<String, String> toParamMap(HttpServletRequest req) {
        //用于存储查询的条件
        Map<String,String> paramMap = new HashMap<>();
        //分页页码
        String pageIndexStr = req.getParameter("pageIndex");
        paramMap.put("pageIndex", pageIndexStr == null ? "1" : pageIndexStr);

            String username = req.getParameter("username");
        if(MyUtils.isNotEmpty( username )){
            paramMap.put("username",username);
        }
                String rolech = req.getParameter("rolech");
        if(MyUtils.isNotEmpty( rolech )){
            paramMap.put("rolech",rolech);
        }
                String content = req.getParameter("content");
        if(MyUtils.isNotEmpty( content )){
            paramMap.put("content",content);
        }
                if(MyUtils.isNotEmpty(req.getParameter("createtimeL"))){
            paramMap.put("createtimeL",req.getParameter("createtimeL"));
        }
        if(MyUtils.isNotEmpty(req.getParameter("createtimeR"))){
            paramMap.put("createtimeR",req.getParameter("createtimeR"));
        }
                String status = req.getParameter("status");
        if(MyUtils.isNotEmpty( status )){
            paramMap.put("status",status);
        }
            //获取需要排序的字段
        String[] orderby = req.getParameterValues("orderby");
        if(orderby!=null){
            String orderbyStr = Arrays.toString(orderby);
            paramMap.put("orderby",orderbyStr.substring(1,orderbyStr.length()-1));
        }
        return paramMap;
    }

}

